﻿/// <reference path="../../js/utils.js" />
'use strict';

define([
    'backbone',
    'screenView',

    'common/models/driver.model',
    'common/models/travel.model',
    'common/models/travels.coll',
    'common/views/driverDetails/driverDetails.view',

    'backgrid',

    'typeahead',
],
function (Backbone, ScreenView, DriverModel, TravelModel, TravelsColl, DriverDetailsView, Backgrid) {
    var View = ScreenView.extend({
        super: null,

        name: 'driver',
        title: 'Choferes',

        initialize: function () {
            this.super = View.__super__;

            return this.super.initialize.apply(this, arguments);
        },

        render: function () {
            this.super.render.apply(this);

            /* First render actions. */
            if (!this.rendered) {
                this.driverDetails = new DriverDetailsView({
                    el: this.$el.find('#details'),
                    /* Pass a clone to the view model to prevent references. */
                    model: new DriverModel({
                        id: '25.678.546',
                        name: 'Nicolas',
                        lastName: 'Lareu',
                        age: 35,
                        tel: '15-6707-4543',
                        email: 'nlareu@gmail.com',
                        address: 'Binnon',
                        addressNumber: 1581,
                        department: '2',
                        departmentNumber: 'C',
                        locality: 'Adrogue',
                        city: 'Almirante Brown',
                    }),
                });
                this.driverDetails.render();



                var travelsColl = new TravelsColl();

                this.driverTravelsGrid = App.utils.grids.travels.driver(travelsColl);
                this.driverTravelsGrid.render();
                // Render the grid and attach the Grid's root to your HTML document
                this.$el.find('#travels').append(this.driverTravelsGrid.el);


                travelsColl.set([
                    new TravelModel({ end: null, }),
                    new TravelModel(),
                    new TravelModel(),
                    new TravelModel(),
                    new TravelModel(),
                ]);



                var eventModel = Backbone.Model.extend({
                    defaults: {
                        description: 'Registro',
                        nextDatetime: '2015-06-07',
                        owner: 'Nicolas Lareu',
                        previousDatetime: '2015-02-24',
                        reminderDatetime: '2015-05-07',
                        repeatFreq: 'Anual',
                        status: 0,
                    },
                });

                var eventsColl = Backbone.Collection.extend({
                    defaults: {
                        model: eventModel,
                    }
                });

                var col = new eventsColl();

                this.driverEventsGrid = App.utils.grids.events.driver(col);;
                this.driverEventsGrid.render();
                // Render the grid and attach the Grid's root to your HTML document
                this.$el.find('#events').append(this.driverEventsGrid.el);

                col.set([
                    new eventModel({ status: 1, }),
                    new eventModel({ repeatFreq: 'Mensual', }),
                    new eventModel({ status: 2, }),
                    new eventModel({ repeatFreq: 'Cada 3 meses', }),
                    new eventModel(),
                ]);



                this.$el.find('#searchDriverTbx').typeahead({
                    source: ['Nicolas Lareu', 'Martin Suez', 'Carlos Martinez'],
                });



                /* Indicates that the view was rendered at least one time. */
                this.rendered = true;
            }
            /* Not first render actions. */
            else {
                
            }


            return this;
        },
    });

    return View;
});